package weaver.interfaces.its.cronjob;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.its.EmailUtil;
import weaver.interfaces.schedule.BaseCronJob;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2025/1/15
 * @Description 责任人1节点提醒
 * @Author AdminZm
 */
public class ZrrOneNodeRemindCronJob extends BaseCronJob {

    BaseBean baseBean = new BaseBean();

    @Override
    public void execute() {
        RecordSet rs = new RecordSet();

        String workflowId = rs.getPropValue("itest_semi_zmnj_prop", "dueDateRemind_workflowId");
        String mainTable = rs.getPropValue("itest_semi_zmnj_prop", "dueDateRemind_mainTable");
        String zrrOneNodeId = rs.getPropValue("itest_semi_zmnj_prop", "zrr_one_node_id");
        String workflowUrl = rs.getPropValue("itest_semi_zmnj_prop", "dueDateRemind_workflowUrl");

        // 获取当前年份
        Year currentYear = Year.now();

        // 获取上一年份
        Year previousYear = currentYear.minusYears(1);

        // 将年份转换为字符串
        String currentYearString = currentYear.toString();
        String previousYearString = previousYear.toString();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        String rqSql = "select * from HrmPubHoliday where countryid = 1 and (substr(holidaydate, 1, 4) = " + currentYearString
                + " or substr(holidaydate, 1, 4) = " + previousYearString + ")";
        Set<String> jrSet = new HashSet<>();
        Set<String> gzrSet = new HashSet<>();
        Set<String> xxrSet = new HashSet<>();
        rs.execute(rqSql);
        while (rs.next()) {
            String holidaydate = rs.getString("holidaydate");
            String changetype = rs.getString("changetype");
            if (StringUtils.isEmpty(holidaydate) || StringUtils.isEmpty(changetype)) {
                continue;
            }
            if (StringUtils.equals(changetype, "1")) {
                jrSet.add(holidaydate);
            }
            if (StringUtils.equals(changetype, "2")) {
                gzrSet.add(holidaydate);
            }
            if (StringUtils.equals(changetype, "3")) {
                xxrSet.add(holidaydate);
            }
        }

        // 获取今天日期，节假日、休息日不提醒
        LocalDate today = LocalDate.now();
        if ((!CollectionUtils.isEmpty(jrSet)) && jrSet.contains(today.toString())) {
            return;
        }
        if ((!CollectionUtils.isEmpty(xxrSet)) && xxrSet.contains(today.toString())) {
            return;
        }
        DayOfWeek todayOfWeek = today.getDayOfWeek();
        if (todayOfWeek == DayOfWeek.SATURDAY || todayOfWeek == DayOfWeek.SUNDAY) {
            if (CollectionUtils.isEmpty(gzrSet) || !gzrSet.contains(todayOfWeek.toString())) {
                return;
            }
        }

        String sql = "select DISTINCT a.requestid,a.requestname,f.nodeid,f.receivedate,f.receivetime,\n" +
                " c.sqr,c.sqrjl,c.zrr,c.zrrjl,h.email as sqrEm,h1.email as sqrjlEm,h2.email as zrrEm,h3.email as zrrjlEm\n" +
                " from workflow_requestbase a\n" +
                " LEFT JOIN " + mainTable + " c on c.requestid=a.requestid\n" +
                " LEFT JOIN workflow_nownode e on e.requestid=a.requestid\n" +
                " LEFT JOIN  workflow_currentoperator f on f.requestid=e.requestid \n" +
                "  and nodeid=e.nownodeid\n" +
                " LEFT JOIN HRMRESOURCE h on h.id=c.sqr\n" +
                " LEFT JOIN HRMRESOURCE h1 on h1.id=c.sqrjl\n" +
                " LEFT JOIN HRMRESOURCE h2 on h2.id=c.zrr\n" +
                " LEFT JOIN HRMRESOURCE h3 on h3.id=c.zrrjl\n" +
                " where a.workflowid=" + workflowId + " and a.currentnodetype!=3 and f.nodeid = " + zrrOneNodeId;
        rs.execute(sql);
        while (rs.next()) {
            try {
                String requestid = rs.getString("requestid");
                String receivedate = rs.getString("receivedate");
                if (StringUtils.isEmpty(receivedate)) {
                    continue;
                }
                LocalDate date = LocalDate.parse(receivedate, formatter);
                boolean isGoing = true;
                while (isGoing) {
                    date = date.plusDays(1);
                    if ((!CollectionUtils.isEmpty(jrSet)) && jrSet.contains(date.toString())) {
                        continue;
                    }
                    if ((!CollectionUtils.isEmpty(xxrSet)) && xxrSet.contains(date.toString())) {
                        continue;
                    }
                    DayOfWeek dayOfWeek = date.getDayOfWeek();
                    if (dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY) {
                        if ((!CollectionUtils.isEmpty(gzrSet)) && gzrSet.contains(date.toString())) {
                            isGoing = false;
                        }
                    } else {
                        isGoing = false;
                    }
                }
                isGoing = true;
                while (isGoing) {
                    date = date.plusDays(1);
                    if ((!CollectionUtils.isEmpty(jrSet)) && jrSet.contains(date.toString())) {
                        continue;
                    }
                    if ((!CollectionUtils.isEmpty(xxrSet)) && xxrSet.contains(date.toString())) {
                        continue;
                    }
                    DayOfWeek dayOfWeek = date.getDayOfWeek();
                    if (dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY) {
                        if ((!CollectionUtils.isEmpty(gzrSet)) && gzrSet.contains(date.toString())) {
                            isGoing = false;
                        }
                    } else {
                        isGoing = false;
                    }
                }
                date = date.plusDays(1);
                if (today.compareTo(date) < 0) {
                    continue;
                }

                String requestname = rs.getString("requestname");
                String sqrEm = rs.getString("sqrEm");
                String sqrjlEm = rs.getString("sqrjlEm");
                String zrrEm = rs.getString("zrrEm");
                String zrrjlEm = rs.getString("zrrjlEm");
                Set<String> emSet = new HashSet<>();
                if (StringUtils.isNotEmpty(sqrEm)) {
                    emSet.add(sqrEm);
                }
                if (StringUtils.isNotEmpty(sqrjlEm)) {
                    emSet.add(sqrjlEm);
                }
                if (StringUtils.isNotEmpty(zrrEm)) {
                    emSet.add(zrrEm);
                }
                if (StringUtils.isNotEmpty(zrrjlEm)) {
                    emSet.add(zrrjlEm);
                }
                if (CollectionUtils.isEmpty(emSet)) {
                    continue;
                }
                for (String email : emSet) {
                    Map<String, String> paramMap = new HashMap<>();
                    paramMap.put("email", email);
                    String content = "<p>&nbsp; &nbsp; 流程还未处理，请关注：" +
                            "<a href=" + workflowUrl + requestid + ">" + requestname + "</a>";
                    paramMap.put("content", content);
                    paramMap.put("zt", "流程超时提醒(" + requestname + ")");
                    EmailUtil.sendEmail(paramMap);
                }
            } catch (Exception e) {
                baseBean.writeLog("DueDateRemindCronJob-" + e.getMessage());
            }
        }
    }
}
